package com.symone.webcms.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.symone.webcms.controller.base.BaseController;
import com.symone.webcms.model.base.ResultMessage;
import com.symone.webcms.model.vo.DataPageVO;
import com.xiaoleilu.hutool.util.NumberUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.plugins.Page;
import com.symone.webcms.service.DataPageService;
import com.symone.webcms.model.DataPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpServletRequest;

/**
 *   @description : 单页内容管理
 *   ---------------------------------
 * 	 @author Archer Chu
 *   @since 2019-03-25
 */
@RestController
@Api(value="/dataPage", description="单页内容管理")
@RequestMapping("/api/page")
public class DataPageController extends BaseController {

    private final Logger logger = LoggerFactory.getLogger(DataPageController.class);

    @Autowired
    public DataPageService dataPageService;

    /**
     * @description : 获取单页内容分页列表
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequiresPermissions("data:page:index")
    @RequestMapping(value = "/page", method = RequestMethod.GET)
    @ApiOperation(value="获取单页内容分页列表", notes="获取单页内容分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name="fieldName",value="搜索字段"),
            @ApiImplicitParam(name="keyword",value="关键词"),
            @ApiImplicitParam(name="module",value="模型Code"),
            @ApiImplicitParam(name="pageSize",value="页大小",required=true,defaultValue = "10"),
            @ApiImplicitParam(name="pageNum",value="页数",required=true,defaultValue = "1")
    })
    public ResultMessage page(Integer pageSize,Integer pageNum,String module,String fieldName , String keyword) {
        if(pageNum == null){
            pageNum = 1;
        }
        if(pageSize == null){
            pageSize = 10;
        }

        String title = null;
        String moduleName = null;
        if(fieldName != null) {
            switch (fieldName) {
                case "title":
                    title = keyword;
                    break;
                case "moduleName":
                    moduleName = keyword;
                    break;
            }
        }
        Page<DataPageVO> page = new Page<DataPageVO>(Integer.valueOf(pageNum),Integer.valueOf(pageSize));
        page = dataPageService.findListVO(page, getSessionLanguage().getCode(),module,title,moduleName);

        return ResultMessage.success("获取单页内容的分页列表",page.getRecords(),page.getTotal());
    }

    /**
     * @description : 获取所有单页内容记录
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequiresPermissions("data:page:index")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ApiOperation(value="获取所有单页内容信息", notes="获取所有单页内容信息")
    public ResultMessage list() {
        return ResultMessage.success("获取所有单页内容信息", dataPageService.findAll());
    }

    /**
     * @description : 通过id获取单页内容
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequiresPermissions("data:page:get")
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ApiOperation(value="获取单页内容信息", notes="获取单页内容信息")
    @ApiImplicitParam(name="id",value="单页内容ID",required=true)
    public ResultMessage get(Long id) {
        return ResultMessage.success("获取单页内容信息成功!", dataPageService.selectById(id));
    }

    /**
     * @description : 添加单页内容
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public ResultMessage add(HttpServletRequest request){
        if( dataPageService.add(request,getSessionUser().getUserName())){
            return ResultMessage.success("添加单页内容成功!");
        }else{
            return ResultMessage.fail("添加单页内容失败!");
        }
    }

    /**
     * @description : 更新单页内容信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ResultMessage updateContent(HttpServletRequest request) {
        String id = request.getParameter("id");
        if(id != null && NumberUtil.isInteger(id)){
            if(dataPageService.update(request,getSessionUser().getUserName())) {
                return ResultMessage.success("Content信息更新成功!");
            }else{
                return ResultMessage.fail("Content信息更新失败!");
            }
        }else {
            return ResultMessage.fail("没有找到ID!");
        }
    }

    /**
     * @description : 更新Content信息
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-01-09
     */
    @RequestMapping(value = "/state", method = RequestMethod.POST)
    public ResultMessage update(DataPage dataPage) {
        if(dataPageService.updateById(dataPage)) {
            return ResultMessage.success("Content信息更新成功!");
        }else{
            return ResultMessage.fail("Content信息更新失败!");
        }
    }
    /**
     * @description : 通过id逻辑删除单页内容
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequiresPermissions("data:page:remove_flag")
    @RequestMapping(value = "/removeFlag", method = RequestMethod.PUT)
    @ApiOperation(value="逻辑删除单页内容", notes="逻辑删除单页内容")
    @ApiImplicitParam(name = "id", value = "单页内容Id", required = true )
    public ResultMessage removeFlag(Long id){
        if(dataPageService.deleteFlagById(id)) {
            return ResultMessage.success("逻辑单页内容删除成功！");
        } else {
            return ResultMessage.fail("逻辑单页内容删除失败！");
        }
    }

    /**
     * @description : 通过id逻辑删除单页内容
     * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
     */
    @RequiresPermissions("data:page:remove")
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ApiOperation(value="物理删除单页内容", notes="物理删除单页内容")
    @ApiImplicitParam(name = "id", value = "单页内容Id", required = true )
    public ResultMessage remove(Long id){
        if(dataPageService.deleteObject(id)) {
            return ResultMessage.success("物理删除单页内容成功！");
        } else {
            return ResultMessage.fail("物理删除单页内容失败！");
        }
    }

    /**
    * @description : 清除单页内容记录
    * ---------------------------------
     * @author : Archer Chu
     * @since : Create in 2019-03-25
    */
    @RequiresPermissions("data:page:clear")
    @RequestMapping(value = "/clear", method = RequestMethod.DELETE)
    @ApiOperation(value="清除单页内容记录", notes="清除已被逻辑删除的单页内容记录")
    public ResultMessage clear(){
        int num = dataPageService.clear();
        if(num > 0) {
            return ResultMessage.success("已经清理了 "+ num +" 条记录！");
        } else {
            return ResultMessage.fail("没有单页内容记录被清除！");
        }
    }



}
